#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PySFML import *
import math

class Button:
    def __init__(self, caption, font, size):
        self.caption = caption
        self.font = font
        self.size = size
    
    def compute(self, rect):
        self.rect = rect
        self.shape = sf.Shape()
        self.shape.SetOutline(2.0)
        for i in range(9):
            self.shape.AddPoint(rect[0] + 12 - math.sin(i * math.pi / 16) * 12, rect[1] + 12 - math.cos(i * math.pi / 16) * 12, sf.Color(0, 0, 0, 64), sf.Color(0, 0, 0, 12))
        for i in range(9):
            self.shape.AddPoint(rect[0] + 12 - math.cos(i * math.pi / 16) * 12, rect[1] + rect[3] - 12 + math.sin(i * math.pi / 16) * 12, sf.Color(0, 0, 0, 64), sf.Color(0, 0, 0, 12))
        for i in range(9):
            self.shape.AddPoint(rect[0] + rect[2] - 12 + math.sin(i * math.pi / 16) * 12, rect[1] + rect[3] - 12 + math.cos(i * math.pi / 16) * 12, sf.Color(0, 0, 0, 64), sf.Color(0, 0, 0, 12))
        for i in range(9):
            self.shape.AddPoint(rect[0] + rect[2] - 12 + math.cos(i * math.pi / 16) * 12, rect[1] + 12 - math.sin(i * math.pi / 16) * 12, sf.Color(0, 0, 0, 64), sf.Color(0, 0, 0, 12))
        
        self.string = sf.String(self.caption, self.font, self.size)
        self.string.SetPosition(rect[0] + rect[2] / 2.0, rect[1] + rect[3] / 2.0)
        self.string.SetCenter(self.string.GetRect().GetWidth() / 2.0, self.string.GetSize() / 1.5)
        self.string.SetColor(sf.Color(255, 255, 255, 255))
    
    def draw(self, window):
        window.Draw(self.shape)
        window.Draw(self.string)
    
    def active(self):
        return True
    
    def focus(self):
        self.string.SetText("focus")
        return self
    
    def blur(self):
        self.string.SetText("blur")
        
